/** @format */
import zhCNAntd from 'antd/lib/locale/zh_CN'
import enUSAntd from 'antd/lib/locale/en_US'
import dataAll from './langsAll.json'

const localLang = localStorage.getItem('langs')
// 线上使用
export let langs: any = localLang ? JSON.parse(localLang) : JSON.parse(JSON.stringify(dataAll))
// 调试使用
// export let langs: any = JSON.parse(JSON.stringify(dataAll))

// console.log(langs, '???')

// 每次刷新页面 判断语言是否需要更新 根据版本判断
export const updateLangs = async (str: string) => {
  const langsData = localStorage.getItem('langs')
  if (!str || !langsData) return
  const langsOld = JSON.parse(langsData)
  const src = `${process.env.FTP_SOURCE_STATIC}${str}`
  const res = await (await fetch(src)).json()
  // if (!langsOld?.version || !res?.version) {
  //   console.log(res)
  //   console.log(langsOld?.version, res?.version, '其中有个没有值')
  // }
  if (res && (!langsOld?.version || !res?.version || langsOld?.version !== res?.version)) {
    console.log('更新线上的数据到本地！')
    langs = res
    localStorage.removeItem('langs')
    localStorage.setItem('langs', JSON.stringify(res))
    location.reload()
  }
}

export const langsOptions: any = []

const keys = Object.keys(langs)
for (const key of keys) {
  if (key !== 'version') {
    langsOptions.push({
      label: langs[key].name,
      value: key,
      key: key,
    })
  }
}

const langsAntd = {
  'zh-CN': zhCNAntd,
  'en-US': enUSAntd,
}
let i = 0
const getErrorRes = (key: string, page?: string) => {
  console.log('未找到关键词' + key, 'error')
  const txt = !!page ? page : 'global'
  console.log('0.0', '请检查' + key + '是否在' + txt + '模块中使用')
  return 'error' + i++
}

// 快速获取对应值的函数
export const getLangTxt = (lang: 'zh-CN' | 'en-US' = 'zh-CN') => {
  return (page: string, key?: string) => {
    // 可省略 global
    if (!key) {
      return langs?.[lang]?.global?.[page] ? langs[lang].global[page] : getErrorRes(page)
    }
    return langs?.[lang]?.[page]?.[key] ? langs[lang][page][key] : getErrorRes(key, page)
  }
}

const lang = localStorage.getItem('lang') || 'zh-CN'
window.getTargetName = getLangTxt(lang as any)

// 重新定义 getTargetName
export const againGetLang = () => {
  // @ts-ignore
  window.getTargetName = null
  const lang = localStorage.getItem('lang') || 'zh-CN'
  console.log(lang, '当前语言？')
  window.getTargetName = getLangTxt(lang as any)
}

// 处理手动清除缓存导致语言包丢失问题 登录后根据是否有语言包 更新最新数据
export const getNewLangs = async (str: string) => {
  // 未上传文件 本地有储存 不需要更新
  if (!str || localStorage.getItem('langs')) return
  const src = `${process.env.FTP_SOURCE_STATIC}${str}`
  const res = await (await fetch(src)).json()
  if (res) {
    langs = res
    againGetLang()
    localStorage.removeItem('langs')
    localStorage.setItem('langs', JSON.stringify(res))
    location.reload()
  }
}

export const getAntdLang = () => {
  return langsAntd[lang]
}

// 输出json 数据 new
// console.log(langs)

export type getFnType = (page: string, key?: string) => any

// window?.getTargetName('manage', 'inspectResult1')
// window?.getTargetName('menu', 'cameraManagement')
// window?.getTargetName('home', 'pleaseEquipment')
// window?.getTargetName('global', 'serialNumber') => window?.getTargetName('pointPositioning')
// window?.getTargetName('route', 'home')
